<?php

class Profile_Model_Avatar
{
	protected static $_cache = array();


	public static function getFileName($objClassIdName, $objId, $imgSize = 48) {
		return self::get($objClassIdName, $objId, $imgSize)->fileName;
	}
	public static function getFilePath($objClassIdName, $objId, $imgSize = 48) {
		return self::get($objClassIdName, $objId, $imgSize)->filePath;
	}
	public static function getFileUrl($objClassIdName, $objId, $imgSize = 48) {
		return self::get($objClassIdName, $objId, $imgSize)->fileUrl;
	}


	protected static function build($objClassIdName, $objId, $imgSize) {
		$obj = Euso_Object::getInstance($objClassIdName, $objId);
		$info = array();
		if (file_exists(FILES_PATH .'/avatar/'. $objClassIdName .'-'. $obj->getIdName() .'-'. $imgSize .'.png')) {
			$info['fileName'] = $objClassIdName .'-'. $obj->getIdName() .'-'. $imgSize .'.png';
		} else if (file_exists(FILES_PATH .'/avatar/__'. $objClassIdName .'-default-'. $imgSize .'.png')) {
			$info['fileName'] = '__'. $objClassIdName .'-default-'. $imgSize .'.png';
		} else if (file_exists(FILES_PATH .'/avatar/__default-'. $imgSize .'.png')) {
			$info['fileName'] = '__default-'. $imgSize .'.png';
		}
		if (empty($info['fileName']) === FALSE) {
			$info['filePath'] = FILES_PATH .'/avatar/'. $info['fileName'];
			$info['fileUrl'] = FILES_URL_BASE .'/avatar/'. $info['fileName'];
		}
		self::$_cache[$objClassIdName][$objId][$imgSize] = (object)$info;
	}

	protected static function get($objClassIdName, $objId, $imgSize) {
		if (isset(self::$_cache[$objClassIdName]) && isset(self::$_cache[$objClassIdName][$objId])
			&& isset(self::$_cache[$objClassIdName][$objId][$imgSize])) {
		} else {
			self::build($objClassIdName, $objId, $imgSize);
		}
		return self::$_cache[$objClassIdName][$objId][$imgSize];
	}
}
